package com.AUI.components
{
	import com.AUI.*;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.text.*;
	/**
	 * Alert dialog
	 * @author HuangMinPing
	 * @site www.AminLab.com
	 */
	public class Alert extends AComponent
	{
		/**
		 * The callback function of Okay button. 
		 */
		public var callback_ok:Function = null;
		/**
		 * [pointer] pointer of icon node.
		 */
		public var e_ok:Button;
		/**
		 * [pointer] pointer of text node.
		 */
		public var e_message:Label;
		/**
		 * Constructor.
		 * @param vertical Indicates the scrollbar is horizontal or vertical scrolling.
		 */
		public function Alert(label:String = null,okname:String = 'OK')
		{
			// init attributes
			depth = 1;
			align = true;
			// init elements
			AUIPrivate.addPreSelectorNames(this.ename + ' > button');
			e_ok = new Button(okname);
			AUIPrivate.addPreSelectorNames(this.ename + ' > label');
			e_message = new Label(label);
			elements.addChild(e_ok);
			elements.addChild(e_message);
			// init events
			e_ok.addEventListener(MouseEvent.CLICK,onClickOk,false,0,true);
			
			/**************** INITCOMPLETE ****************/
			Object(this).constructor == Alert && initComplete();
		}
		/**
		 * @private
		 */
		override protected function resizeBoxModel():void
		{
			// update placement
			var V:int;
			var e1:DisplayObject;
			var e2:DisplayObject;
			switch(drawStyle.font.placement)
			{
				case 'right':
					V = 0; e1 = e_ok; e2 = e_message;
					break;
				case 'left':
					V = 0; e1 = e_message; e2 = e_ok;
					break;
				case 'bottom':
					V = 1; e1 = e_ok; e2 = e_message;
					break;
				case 'top':
					V = 1; e1 = e_message; e2 = e_ok;
					break;
			}
			var EW1:int = e1.width;
			var EH1:int = e1.height;
			var EW2:int = e2.width;
			var EH2:int = e2.height;
			var EG:int; // node gap
			var EM:int; // node max attribute value
			if(V) // vertical
			{
				EG   = (EH1 && EH2) ? drawStyle.gap.v : 0;
				EM   = EW1 > EW2 ? EW1 : EW2;
				e1.x = int((EM - EW1) / 2);
				e2.x = int((EM - EW2) / 2);
				e1.y = 0;
				e2.y = EH1 + EG;
			}
			else
			{
				EG   = (EW1 && EW2) ? drawStyle.gap.h : 0;
				EM   = EH1 > EH2 ? EH1 : EH2;
				e1.x = 0;
				e2.x = EW1 + EG;
				e1.y = int((EM - EH1) / 2);
				e2.y = int((EM - EH2) / 2);
			}
			// super do
			super.resizeBoxModel();
		}
		/**
		 * Called when MouseEvent.CLICK event has been dispatched on the ok button of component.
		 */
		protected function onClickOk(e:MouseEvent):void
		{
			callback_ok && callback_ok();
		}
	}
}